//
//  YLRiskAssessmentController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/19.
//  Copyright © 2017年 she1323. All rights reserved.
//  风险测试

#import "YLRiskAssessmentController.h"
#import "YLGlobeHeader.h"
#import <Masonry.h>
#import "YLRiskAssessmentBaseView.h"
#import "YLAssessmentProgress.h"
#import "YLRiskAssessmentViewModel.h"
#import "YLEvaluationResultController.h"
#import "KLTNavigationController.h"
#import "YLMineViewModel.h"
@interface YLRiskAssessmentController ()<YLRiskAssessmentBaseViewDelegate>

@property (nonatomic, strong) YLRiskAssessmentViewModel *viewModel;
@property (nonatomic, weak) YLAssessmentProgress *pregress;
@property (nonatomic, weak) YLRiskAssessmentBaseView *baseView;

@property (nonatomic, strong) YLMineViewModel *myInfoViewModel;

/**
 根据数字得出字母
 */
@property (nonatomic, strong) NSDictionary *numberToLetterDict;


/**
 答案数组
 */
@property (nonatomic, strong) NSMutableArray *answerArray;

@end

@implementation YLRiskAssessmentController

- (NSMutableArray *)answerArray
{
    if (!_answerArray) {
        _answerArray = [NSMutableArray arrayWithCapacity:16];
    }
    return _answerArray;
}

- (YLMineViewModel *)myInfoViewModel
{
    if (!_myInfoViewModel) {
        _myInfoViewModel = [[YLMineViewModel alloc] init];
    }
     return  _myInfoViewModel;
}

- (NSDictionary *)numberToLetterDict
{
    if (!_numberToLetterDict) {
        _numberToLetterDict = @{@"1" : @"A",
                                @"2" : @"B",
                                @"3" : @"C",
                                @"4" : @"D",
                                @"5" : @"E",
                                @"6" : @"F",
                                @"7" : @"g"
                                };
    }
    return _numberToLetterDict;
}

- (YLRiskAssessmentViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLRiskAssessmentViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    KLTNavigationController *nav = (KLTNavigationController *)self.navigationController;
    nav.panGestureRec.enabled = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"风险测评";
    
    self.view.backgroundColor = YLColor(240, 240, 247);
    
    __weak typeof(self) weakSelf = self;
    
    YLRiskAssessmentBaseView *baseView = [[YLRiskAssessmentBaseView alloc] init];
    
    baseView.delegate = self;
    
    _baseView = baseView;
    
    baseView.questionNum = self.viewModel.groups.count;
    
    baseView.group = self.viewModel.groups.firstObject;
    
    [self.view addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view).mas_offset(60);
        make.left.mas_equalTo(weakSelf.view).mas_offset(20);
        make.right.mas_equalTo(weakSelf.view).mas_offset(-20);
        make.bottom.mas_equalTo(weakSelf.view).mas_offset(-60);
    }];
    
    
    YLAssessmentProgress *progress = [[YLAssessmentProgress alloc] init];
    progress.totalCount = self.viewModel.groups.count;
    progress.currentIndex = 1;
    _pregress = progress;
    [self.view addSubview:progress];
    [progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(baseView.mas_top).mas_offset(-25);
        make.height.mas_equalTo(10);
        make.left.right.mas_equalTo(baseView);
    }];
    
    WHNavBackButton *backButton = [WHNavBackButton navBackButton];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        baseView.userInteractionEnabled = YES;
    }];

}

- (void)back
{
    if (_pregress.currentIndex == self.viewModel.groups.count) {
         [self.navigationController popToRootViewControllerAnimated:NO];
    } else {
        __weak typeof(self) weakSelf = self;
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"本次风险能力评估还未完成," message:@"确定退出" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *quitAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 禁用返回手势
            if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
                weakSelf.navigationController.interactivePopGestureRecognizer.enabled = YES;
            }
            KLTNavigationController *nav = (KLTNavigationController *)weakSelf.navigationController;
            nav.panGestureRec.enabled = YES;
            [weakSelf.navigationController popToRootViewControllerAnimated:NO];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:quitAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _baseView.group = self.viewModel.groups.firstObject;
    _pregress.currentIndex = 1;
}

- (void)riskAssessmentBaseView:(YLRiskAssessmentBaseView *)baseView titleNumber:(NSInteger)titleNumber answerNumber:(NSInteger)answerNumber
{
    // 点击下一题传进来
    // 因为 第一个 titleNumber 为 1  而数组下标为0 可以根据titleNumber直接得到下一个问题
   // NSLog(@"选择了第%ld题的第%ld个选项", titleNumber, answerNumber);
   
    
    if (self.viewModel.groups.count == titleNumber) {
        
    } else {
        baseView.group = self.viewModel.groups[titleNumber];
        _pregress.currentIndex = titleNumber + 1;
    }
    
    
   NSString *questionAnswer = [self.numberToLetterDict objectForKey:[NSString stringWithFormat:@"%zd", answerNumber]];
    NSDictionary *answer = @{
                             @"QuestionNomber" : @(titleNumber),
                             @"QuestionAnswer" : questionAnswer
                             };
    
    if (self.answerArray.count < self.viewModel.groups.count) {
         [self.answerArray addObject:answer];
    }
    
   
    if (titleNumber >= self.viewModel.groups.count - 1) {
        [baseView finsh];
    }
}

- (void)riskAssessmentBaseViewSummitAnswer:(YLRiskAssessmentBaseView *)baseView
{
    baseView.userInteractionEnabled = NO;
        
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.summitCommand execute:self.answerArray] subscribeNext:^(id  _Nullable x) {
        [weakSelf loadDataWithView:baseView];
        
    }];
}

- (void)loadDataWithView:(YLRiskAssessmentBaseView *)baseView
{
    __weak typeof(self) weakSelf = self;
    [[self.myInfoViewModel.myInfoCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        baseView.userInteractionEnabled = YES;
        baseView.showNext = YES;
        YLEvaluationResultController *evaluationResult = [[YLEvaluationResultController alloc] init];
        evaluationResult.answerModel = weakSelf.viewModel.anseweModel;
        [weakSelf.navigationController pushViewController:evaluationResult animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
